package saucaca.JSON;

import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;

import lemon.kenhnhacvn.BaseEngine;
import lemon.kenhnhacvn.BaseActivity;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import saucaca.Util.CMD;
import saucaca.Util.TagName;
import android.util.Log;

public class WServiceLib {
	public static final String IS3G = BaseActivity.IS3G;
	public static final String ERR = "kenhnhac.vn";
	private static final String CODE = "Code";
	public static final String URL_HOME = "http://service1.kenhnhac.vn:7070/Controllers/ServiceCore.asmx";
	public final static String NAMESPACE = "http://tempuri.org/";
	public final static String METHOD = "ProService";
	public final static String TAG_KENHNHAC = "Androi_Kenhnhac";
	public final static String STR_HEADER = "{'cmd':'%s','ip':'" + BaseActivity.IP + "','agent':'android','token':'%s'}";
	public static final String HEADER = "Header";
	public static final String BODY = "Body";
	public static final String FOOTER = "Footer";
	public final static String STR_FOOTER = "{'user':'mj','password':'jj'}";

	public static String getResponse(List<BasicNameValuePair> params) {
		try {
			SoapObject request = new SoapObject(NAMESPACE, METHOD);
			for (int i = 0; i < params.size(); i++) {
				request.addProperty(params.get(i).getName(), params.get(i).getValue());
			}
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			envelope.dotNet = true;
			envelope.setOutputSoapObject(request);

			HttpTransportSE httpTrans = new HttpTransportSE(URL_HOME, 15000);
			if (httpTrans.getServiceConnection().getResponseCode() != HttpURLConnection.HTTP_OK) {
				httpTrans.getServiceConnection().connect();
			}

			// httpTrans.debug = true;
			// System.setProperty("http.keepAlive", "false");
			// httpTrans.getServiceConnection().connect();
			httpTrans.call(NAMESPACE + METHOD, envelope);
			SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
			return result.toString().trim();
		} catch (Exception e) {
			Log.e(TAG_KENHNHAC, "err at getResponse-----" + e.getMessage());
			return null;
		}
	}

	private static String checkTypehot(String type) {
		if (isInteger(type)) {
			int x = Integer.parseInt(type);
			switch (x) {
			case 1:
				return "1";
			case 2:
				return "2";
			case 3:
				return "3";
			default:
				return "0";
			}
		} else
			return "0";
	}
	
	private static String checkString(String str) {
		if (str.equals(""))
			return "ALL";
		return str;
	}

	private static String checkPage(String page) {
		if (page.equals(""))
			return "0";
		return page;
	}

	private static String checkPageSize(String pageSize) {
		if (pageSize.equals(""))
			return "20";
		return pageSize;
	}

	public static boolean isInteger(String input) {
		try {
			Integer.parseInt(input);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	public static String convertSingerID(String id) {
		String s = "";
		if(id=="")
			return "0";
		try {
			int x = (int) Float.parseFloat(id);
			s = Integer.toString(x);
		} catch (Exception e) {

		}
		return s;
	}
	public static String getHomeString() {
		String STR_HEADER = "{'cmd':'%s','ip':'" + BaseActivity.getLocalIpAddress() + "','agent':'android','token':'%s'}";
		String header = String.format(STR_HEADER, CMD.CMD_HOME, JsonReader.getToken());
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair(HEADER, header));
		params.add(new BasicNameValuePair(BODY, ""));
		params.add(new BasicNameValuePair(FOOTER, STR_FOOTER));
		Log.e(TAG_KENHNHAC, "header" + header);
		Log.e(TAG_KENHNHAC, "STR_FOOTER" + STR_FOOTER);
		try {
			SoapObject request = new SoapObject(NAMESPACE, METHOD);
			for (int i = 0; i < params.size(); i++) {
				request.addProperty(params.get(i).getName(), params.get(i).getValue());
			}
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			envelope.dotNet = true;
			envelope.setOutputSoapObject(request);

			HttpTransportSE httpTrans = new HttpTransportSE(URL_HOME, 20000);
			if (httpTrans.getServiceConnection().getResponseCode() != HttpURLConnection.HTTP_OK) {
				httpTrans.getServiceConnection().connect();
			}
			// httpTrans.debug = true;
			System.setProperty("http.keepAlive", "false");
			// httpTrans.getServiceConnection().connect();
			httpTrans.call(NAMESPACE + METHOD, envelope);
			SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
			return result.toString().trim();
		} catch (Exception e) {
			Log.e(TAG_KENHNHAC, "err at getResponse-----" + e.getMessage());
			return null;
		}
	}
	public static String register(String user, String pass) {
		String result = null;
		try {
			String header = String.format(STR_HEADER, CMD.CMD_ACC_REGISTER, JsonReader.getToken());
			String body = String.format("{'userName':'%s', 'password':'%s','is3G':'%s'}", user, pass, IS3G);
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			params.add(new BasicNameValuePair(HEADER, header));
			params.add(new BasicNameValuePair(BODY, body));
			params.add(new BasicNameValuePair(FOOTER, STR_FOOTER));
			String json = getResponse(params);
			if (json != null) {
				result = new JSONObject(json).getJSONObject(HEADER).getString(CODE);
			}
		} catch (Exception e) {
			Log.e(ERR, "err at register-----" + e.getMessage());
			e.printStackTrace();
		}

		return result;
	}

	public static String forgetpass(String user) {
		String result = null;
		try {
			String header = String.format(STR_HEADER, CMD.CMD_ACC_FORGET_PASS, JsonReader.getToken());
			String body = String.format("{'userName':'%s'}", user);
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			params.add(new BasicNameValuePair(HEADER, header));
			params.add(new BasicNameValuePair(BODY, body));
			params.add(new BasicNameValuePair(FOOTER, STR_FOOTER));
			result = getResponse(params);
		} catch (Exception e) {
			Log.e(ERR, "err at forgetpass-----" + e.getMessage());
			e.printStackTrace();
		}
		return result;
	}

	public static String login(String tel, String pass) {
		String result = null;
		try {
			String header = String.format(STR_HEADER, CMD.CMD_ACC_LOGIN, JsonReader.getToken());
			String body = String.format("{'userName':'%s', 'password':'%s','is3G':'%s'}", tel, pass, IS3G);
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			params.add(new BasicNameValuePair(HEADER, header));
			params.add(new BasicNameValuePair(BODY, body));
			params.add(new BasicNameValuePair(FOOTER, STR_FOOTER));
			result = getResponse(params);
		} catch (Exception e) {
			Log.e(ERR, "err at login-----" + e.getMessage());
			e.printStackTrace();
		}

		return result;
	}

	public static String listAlbum(String cateCode, String singerCode, String areaCode, String page, String pageSize, String isAlbum) {
		String str = null;
		try {
			String header = String.format(STR_HEADER, "3097", JsonReader.getToken());
			String STR_BODY = "{'page':'%s','pageSize':'%s','cateCode':'%s','singerId':'%s','areaCode':'%s','isAlbum':'%s'}";
			String body = String.format(STR_BODY, checkPage(page), checkPageSize(pageSize), checkString(cateCode), convertSingerID(singerCode), checkString(areaCode),
					checkTypehot(isAlbum));
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			params.add(new BasicNameValuePair(HEADER, header));
			params.add(new BasicNameValuePair(BODY, body));
			params.add(new BasicNameValuePair(FOOTER, STR_FOOTER));
			str = getResponse(params);
		} catch (Exception e) {
			Log.e(ERR, "err at listAlbum-----" + e.getMessage());
			e.printStackTrace();
		}
		return str;
	}

	public static String listArtistAlbum(String page, String pageSize, String areaCode, String singerCode, String isAlbum, String cateCode) {
		String str = null;
		try {
			String header = String.format(STR_HEADER, "3097", JsonReader.getToken());
			String STR_BODY = "{'page':'%s','pageSize':'%s','areaCode':'%s','singerId':'%s','isAlbum':'0','cateCode':'%s'}";
			String body = String.format(STR_BODY, checkPage(page), checkPageSize(pageSize), checkString(areaCode), convertSingerID(singerCode), checkString(areaCode),
					checkTypehot(isAlbum), checkString(cateCode));
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			params.add(new BasicNameValuePair(HEADER, header));
			params.add(new BasicNameValuePair(BODY, body));
			params.add(new BasicNameValuePair(FOOTER, STR_FOOTER));
			str = getResponse(params);
		} catch (Exception e) {
			Log.e(ERR, "err at Songs-----" + e.getMessage());
			e.printStackTrace();
		}
		return str;
	}

	public static String listSongVideoAlbum(String sType, String cateCode, String singerCode, String authorCode, String areaCode, String page, String pageSize, String typeHot) {
		String str = null;
		try {
			String header = String.format(STR_HEADER, "3014", JsonReader.getToken());
			String STR_BODY = "{'page':'%s','pageSize':'%s','sType':'%s', 'cateCode':'%s','singerId':'%s','authorCode':'%s','areaCode':'%s','type':'%s'}";
			String body = String.format(STR_BODY, checkPage(page), checkPageSize(pageSize), sType, checkString(cateCode), convertSingerID(singerCode), checkString(authorCode),
					checkString(areaCode), checkTypehot(typeHot));
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			params.add(new BasicNameValuePair(HEADER, header));
			params.add(new BasicNameValuePair(BODY, body));
			params.add(new BasicNameValuePair(FOOTER, STR_FOOTER));
			str = getResponse(params);
		} catch (Exception e) {
			Log.e(ERR, "err at Songs-----" + e.getMessage());
			e.printStackTrace();
		}
		return str;
	}

	public static String Bxh(String btype, String areaCode, String week, String page, String pageSize) {
		String str = null;
		try {
			String STR_BODY = "{'btype':'%s','areaCode':'%s','week':'%s','page':'%s','pageSize':'%s'}";
			String header = String.format(STR_HEADER, "3084", JsonReader.getToken());
			String body = String.format(STR_BODY, btype, checkString(areaCode), isInteger(week) ? week : "0", checkPage(page), checkPageSize(pageSize));
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			params.add(new BasicNameValuePair(HEADER, header));
			params.add(new BasicNameValuePair(BODY, body));
			params.add(new BasicNameValuePair(FOOTER, STR_FOOTER));
			str = getResponse(params);
		} catch (Exception e) {
			Log.e(ERR, "err at album-----" + e.getMessage());
			e.printStackTrace();
		}

		return str;
	}

	public static String getTop(String CMD) {
		String str = null;
		try {
			// {'page':'','pageSize':'','btype':'1','areaCode':'ALL','week':'47'}
			String header = String.format(STR_HEADER, CMD, JsonReader.getToken());
			String body = String.format("{'top':'10'}");
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			params.add(new BasicNameValuePair(HEADER, header));
			params.add(new BasicNameValuePair(BODY, body));
			params.add(new BasicNameValuePair(FOOTER, STR_FOOTER));
			str = getResponse(params);
		} catch (Exception e) {
			Log.e(ERR, "err at album-----" + e.getMessage());
			e.printStackTrace();
		}

		return str;
	}

	public static String getListRingtone(String page, String pageSize, String type, String cateCode, String singerCode, String authorCode, String areaCode, String telCode) {
		String str = null;
		try {
			String header = String.format(STR_HEADER, CMD.CMD_TONE_LIST, JsonReader.getToken());
			String sBody = "{'page':'%s','pageSize':'%s','type':'%s', 'cateCode':'%s','singerId':'%s','authorCode':'%s','areaCode':'%s','telCode':'%s'}";
			TagName.showLogError("Ringtone", "getListRingtone: " + telCode);
			String body = String.format(sBody, checkPage(page), checkPageSize(pageSize), checkTypehot(type), checkString(cateCode), convertSingerID(singerCode),
					checkString(authorCode), checkString(areaCode), telCode);
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			params.add(new BasicNameValuePair(HEADER, header));
			params.add(new BasicNameValuePair(BODY, body));
			params.add(new BasicNameValuePair(FOOTER, STR_FOOTER));
			str = getResponse(params);
		} catch (Exception e) {
			Log.e(ERR, "err at album-----" + e.getMessage());
			e.printStackTrace();
		}

		return str;
	}

	public static String artist(String page, String pageSize, String areaCode) {
		String str = null;
		try {
			String header = String.format(STR_HEADER, "3090", JsonReader.getToken());
			String body = String.format("{'page':'%s','pageSize':'%s','areaCode':'%s'}", checkPage(page), checkPageSize(pageSize), checkString(areaCode));
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			params.add(new BasicNameValuePair(HEADER, header));
			params.add(new BasicNameValuePair(BODY, body));
			params.add(new BasicNameValuePair(FOOTER, STR_FOOTER));
			str = getResponse(params);
		} catch (Exception e) {
			Log.e(ERR, "err at album-----" + e.getMessage());
			e.printStackTrace();
		}

		return str;
	}

	public static String idDetais(String CMD, String id) {
		String str = null;
		try {
			String header = String.format(STR_HEADER, CMD, JsonReader.getToken());
			String body = String.format("{'id':'%s'}", BaseEngine.convertStringLongtoInteger(id));
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			params.add(new BasicNameValuePair(HEADER, header));
			params.add(new BasicNameValuePair(BODY, body));
			params.add(new BasicNameValuePair(FOOTER, STR_FOOTER));
			Log.e(TAG_KENHNHAC, "HEADER: "+header);
			Log.e(TAG_KENHNHAC, "BODY: "+body);
			Log.e(TAG_KENHNHAC, "FOOTER: "+STR_FOOTER);
			
			str = getResponse(params);
		} catch (Exception e) {
			Log.e(ERR, "err at album-----" + e.getMessage());
			e.printStackTrace();
		}

		return str;
	}
	public static String download(String user, String id) {
		String str = null;
		try {
			String header = String.format(STR_HEADER, CMD.CMD_DOWNLOAD, JsonReader.getToken());
			String body = String.format("{'userName':'%s','id':'%s'}",user, BaseEngine.convertStringLongtoInteger(id));
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			params.add(new BasicNameValuePair(HEADER, header));
			params.add(new BasicNameValuePair(BODY, body));
			params.add(new BasicNameValuePair(FOOTER, STR_FOOTER));
			str = getResponse(params);
		} catch (Exception e) {
			Log.e(ERR, "err at album-----" + e.getMessage());
			e.printStackTrace();
		}

		return str;
	}
	public static String artistDetai(String singerCode) {
		String str = null;
		try {
			String header = String.format(STR_HEADER, "3091", JsonReader.getToken());
			String body = String.format("{'singerId':'%s'}", convertSingerID(singerCode));
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			params.add(new BasicNameValuePair(HEADER, header));
			params.add(new BasicNameValuePair(BODY, body));
			params.add(new BasicNameValuePair(FOOTER, STR_FOOTER));
			str = getResponse(params);
		} catch (Exception e) {
			Log.e(ERR, "err at album-----" + e.getMessage());
			e.printStackTrace();
		}

		return str;
	}

	public static String news(String page, String pageSize, String cateCode) {
		String str = null;
		try {
			String header = String.format(STR_HEADER, "3050", JsonReader.getToken());
			String body = String.format("{'page':'%s','pageSize':'%s','cateCode':'%s'}", checkPage(page), checkPageSize(pageSize), checkString(cateCode));
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			params.add(new BasicNameValuePair(HEADER, header));
			params.add(new BasicNameValuePair(BODY, body));
			params.add(new BasicNameValuePair(FOOTER, STR_FOOTER));
			str = getResponse(params);
		} catch (Exception e) {
			Log.e(ERR, "err at album-----" + e.getMessage());
			e.printStackTrace();
		}

		return str;
	}

	public static String playlist(String page, String pageSize, String username) {
		String str = null;
		try {
			String header = String.format(STR_HEADER, "3099", JsonReader.getToken());
			String body = String.format("{'page':'%s','pageSize':'%s','userName':'%s'}", checkPage(page), checkPageSize(pageSize), username);
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			params.add(new BasicNameValuePair(HEADER, header));
			params.add(new BasicNameValuePair(BODY, body));
			params.add(new BasicNameValuePair(FOOTER, STR_FOOTER));
			str = getResponse(params);
		} catch (Exception e) {
			Log.e(ERR, "err at playlist-----" + e.getMessage());
			e.printStackTrace();
		}
		return str;
	}

	public static String newsList(String page, String pageSize) {
		String str = null;
		try {
			String header = String.format(STR_HEADER, "3050", JsonReader.getToken());
			String body = String.format("{'page':'%s','pageSize':'%s','cateCode':'ALL'}", checkPage(page), checkPageSize(pageSize), "ALL");
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			params.add(new BasicNameValuePair(HEADER, header));
			params.add(new BasicNameValuePair(BODY, body));
			params.add(new BasicNameValuePair(FOOTER, STR_FOOTER));
			str = getResponse(params);
		} catch (Exception e) {
			Log.e(ERR, "err at new detail-----" + e.getMessage());
			e.printStackTrace();
		}

		return str;
	}

	public static String newsDetail(String newid) {
		String str = null;
		try {
			String header = String.format(STR_HEADER, "3052", JsonReader.getToken());
			String body = String.format("{'newId':'%s'}", BaseEngine.convertStringLongtoInteger(newid));
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			params.add(new BasicNameValuePair(HEADER, header));
			params.add(new BasicNameValuePair(BODY, body));
			params.add(new BasicNameValuePair(FOOTER, STR_FOOTER));
			str = getResponse(params);
		} catch (Exception e) {
			Log.e(ERR, "err at new detail-----" + e.getMessage());
			e.printStackTrace();
		}

		return str;
	}

	public static String tvShowDetail(String CMD, String page, String pageSize, String cateCode) {
		String str = null;
		try {
			String header = String.format(STR_HEADER, CMD, JsonReader.getToken());
			String body = String.format("{'page':'%s','pageSize':'%s','cateCode':'%s'}", checkPage(page), checkPageSize(pageSize), checkString(cateCode));
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			params.add(new BasicNameValuePair(HEADER, header));
			params.add(new BasicNameValuePair(BODY, body));
			params.add(new BasicNameValuePair(FOOTER, STR_FOOTER));
			str = getResponse(params);
		} catch (Exception e) {
			Log.e(ERR, "err at new tvShowDetail-----" + e.getMessage());
			e.printStackTrace();
		}

		return str;
	}

	public static String playlistAdd(String playId, String songId, String userName, String playName) {
		String str = null;
		try {
			String header = String.format(STR_HEADER, CMD.CMD_PLAYLIST_ADD, JsonReader.getToken());
			String body = String.format("{'playId':'%s','songId':'%s','userName':'%s','album':'%s'}", BaseEngine.convertStringLongtoInteger(playId),
					BaseEngine.convertStringLongtoInteger(songId), userName, playName);
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			params.add(new BasicNameValuePair(HEADER, header));
			params.add(new BasicNameValuePair(BODY, body));
			params.add(new BasicNameValuePair(FOOTER, STR_FOOTER));
			str = getResponse(params);
		} catch (Exception e) {
			Log.e(ERR, "err at new playlistAdd-----" + e.getMessage());
			e.printStackTrace();
		}

		return str;
	}
	public static String playlistDelete(String playId, String songId, String userName) {
		String str = null;
		try {
			String header = String.format(STR_HEADER, CMD.CMD_PLAYLIST_DELETE, JsonReader.getToken());
			String body = String.format("{'playId':'%s','songId':'%s','userName':'%s'}", 
					BaseEngine.convertStringLongtoInteger(playId),
					BaseEngine.convertStringLongtoInteger(songId), userName);
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			params.add(new BasicNameValuePair(HEADER, header));
			params.add(new BasicNameValuePair(BODY, body));
			params.add(new BasicNameValuePair(FOOTER, STR_FOOTER));
			str = getResponse(params);
		} catch (Exception e) {
			Log.e(ERR, "err at new playlistDelete-----" + e.getMessage());
			e.printStackTrace();
		}

		return str;
	}

	public static String playlistDetails(String playId) {
		String str = null;
		try {
			String header = String.format(STR_HEADER, CMD.CMD_PLAYLIST_DETAIL, JsonReader.getToken());
			String body = String.format("{'playId':'%s'}", BaseEngine.convertStringLongtoInteger(playId));
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			params.add(new BasicNameValuePair(HEADER, header));
			params.add(new BasicNameValuePair(BODY, body));
			params.add(new BasicNameValuePair(FOOTER, STR_FOOTER));
			str = getResponse(params);
		} catch (Exception e) {
			Log.e(ERR, "err at new playlistAdd-----" + e.getMessage());
			e.printStackTrace();
		}

		return str;
	}

	public static String listCommnets(String audioId, String btype, String page, String pageSize) {
		String str = null;
		try {
			String header = String.format(STR_HEADER, CMD.CMD_COMMENT_LIST, JsonReader.getToken());
			String body = String.format("{'autoId':'%s','btype':'%s','page':'%s','pageSize':'%s'}", BaseEngine.convertStringLongtoInteger(audioId), btype, checkPage(page),
					checkPageSize(pageSize));
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			params.add(new BasicNameValuePair(HEADER, header));
			params.add(new BasicNameValuePair(BODY, body));
			params.add(new BasicNameValuePair(FOOTER, STR_FOOTER));
			str = getResponse(params);
		} catch (Exception e) {
			Log.e(ERR, "err at new listCommnets-----" + e.getMessage());
			e.printStackTrace();
		}

		return str;
	}

	public static String sendCommnets(String audioId, String btype, String title, String content, String userName) {
		String str = null;
		try {
			String header = String.format(STR_HEADER, CMD.CMD_COMMENT_ADD, JsonReader.getToken());
			String sBody = "{'autoId':'%s','btype':'%s','title':'%s','content':'%s','userName':'%s'}";
			String body = String.format(sBody, BaseEngine.convertStringLongtoInteger(audioId), btype, title, content, userName);
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			params.add(new BasicNameValuePair(HEADER, header));
			params.add(new BasicNameValuePair(BODY, body));
			params.add(new BasicNameValuePair(FOOTER, STR_FOOTER));
			str = getResponse(params);
		} catch (Exception e) {
			Log.e(ERR, "err at new sendCommnets-----" + e.getMessage());
			e.printStackTrace();
		}

		return str;
	}

	public static String search(String word) {
		String str = null;
		try {
			String header = String.format(STR_HEADER, CMD.CMD_SEARCH, JsonReader.getToken());
			String sBody = "{'word':'%s','stype':'0'}";
			String body = String.format(sBody, word);
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			params.add(new BasicNameValuePair(HEADER, header));
			params.add(new BasicNameValuePair(BODY, body));
			params.add(new BasicNameValuePair(FOOTER, STR_FOOTER));
			str = getResponse(params);
		} catch (Exception e) {
			Log.e(ERR, "err at new search-----" + e.getMessage());
			e.printStackTrace();
		}

		return str;
	}

	public static String guide(String code) {
		String str = null;
		try {
			String header = String.format(STR_HEADER, CMD.CMD_GUIDE_DETAIl, JsonReader.getToken());
			String sBody = "{'code':'%s'}";
			String body = String.format(sBody, code);
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			params.add(new BasicNameValuePair(HEADER, header));
			params.add(new BasicNameValuePair(BODY, body));
			params.add(new BasicNameValuePair(FOOTER, STR_FOOTER));
			str = getResponse(params);
		} catch (Exception e) {
			Log.e(ERR, "err at new search-----" + e.getMessage());
			e.printStackTrace();
		}

		return str;
	}

	public static String notification(String page, String pageSize, String type) {
		String str = null;
		try {
			String header = String.format(STR_HEADER, CMD.CMD_NOTIFICATION_LIST, JsonReader.getToken());
			String body = String.format("{'page':'%s','pageSize':'%s','type':'%s'}", checkPage(page), checkPageSize(pageSize), type);
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			params.add(new BasicNameValuePair(HEADER, header));
			params.add(new BasicNameValuePair(BODY, body));
			params.add(new BasicNameValuePair(FOOTER, STR_FOOTER));
			str = getResponse(params);
		} catch (Exception e) {
			Log.e(ERR, "err at notification-----" + e.getMessage());
			e.printStackTrace();
		}
		return str;
	}
	public static String account_info(String userName) {
		String str = null;
		try {
			String header = String.format(STR_HEADER, CMD.CMD_ACC_INFO, JsonReader.getToken());
			String _body="{'userName':'%s'}";
			String body = String.format(_body, userName);
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			params.add(new BasicNameValuePair(HEADER, header));
			params.add(new BasicNameValuePair(BODY, body));
			params.add(new BasicNameValuePair(FOOTER, STR_FOOTER));
			str = getResponse(params);
		} catch (Exception e) {
			Log.e(ERR, "err at account_updateinfo-----" + e.getMessage());
			e.printStackTrace();
		}
		return str;
	}
	public static String account_updateinfo(String userName, String fullName, String sex, String birthDay, String phone) {
		String str = null;
		try {
			String header = String.format(STR_HEADER, CMD.CMD_ACC_UPDATE, JsonReader.getToken());
			String _body="{'userName':'%s','fullName':'%s','sex':'%s','birthDay':'%s','phone':'%s'}";
			String body = String.format(_body, userName, fullName, sex, birthDay, phone);
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			params.add(new BasicNameValuePair(HEADER, header));
			params.add(new BasicNameValuePair(BODY, body));
			params.add(new BasicNameValuePair(FOOTER, STR_FOOTER));
			str = getResponse(params);
		} catch (Exception e) {
			Log.e(ERR, "err at account_updateinfo-----" + e.getMessage());
			e.printStackTrace();
		}
		return str;
	}
	public static String account_changePass(String userName,String passOld, String passNew) {
		String str = null;
		try {
			String header = String.format(STR_HEADER, CMD.CMD_ACC_UPDATE_PASS, JsonReader.getToken());
			String _body="{'userName':'%s', 'passNew':'%s','passOld':'%s'}";
			String body = String.format(_body, userName, passNew,passOld);
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			params.add(new BasicNameValuePair(HEADER, header));
			params.add(new BasicNameValuePair(BODY, body));
			params.add(new BasicNameValuePair(FOOTER, STR_FOOTER));
			str = getResponse(params);
		} catch (Exception e) {
			Log.e(ERR, "err at account_updateinfo-----" + e.getMessage());
			e.printStackTrace();
		}
		return str;
	}
	
	public static String account_uploadAvatar(String avatarbase64,String userName,String isAvatar ) {
		String str = null;
		try {
			String header = String.format(STR_HEADER, CMD.CMD_ACC_UPLOAD_AVATAR, JsonReader.getToken());
			String _body="{'userName':'%s', 'isAvatar':'%s'}";
			String body = String.format(_body, userName, isAvatar);
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			params.add(new BasicNameValuePair("avatar", avatarbase64));
			params.add(new BasicNameValuePair(HEADER, header));
			params.add(new BasicNameValuePair(BODY, body));
			params.add(new BasicNameValuePair(FOOTER, STR_FOOTER));
//			str = getResponse(params);
			try {
				String URL_HOME=
						"http://service1.kenhnhac.vn:7070/Controllers/ServiceCore.asmx?op=uploadAvatar";
				SoapObject request = new SoapObject(NAMESPACE, "uploadAvatar");
				for (int i = 0; i < params.size(); i++) {
					request.addProperty(params.get(i).getName(), params.get(i).getValue());
				}
				SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
				envelope.dotNet = true;
				envelope.setOutputSoapObject(request);

				HttpTransportSE httpTrans = new HttpTransportSE(URL_HOME, 15000);
				if (httpTrans.getServiceConnection().getResponseCode() != HttpURLConnection.HTTP_OK) {
					httpTrans.getServiceConnection().connect();
				}
				// httpTrans.debug = true;
				// System.setProperty("http.keepAlive", "false");
				// httpTrans.getServiceConnection().connect();
				httpTrans.call(NAMESPACE + "uploadAvatar", envelope);
				SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
				str= result.toString().trim();
			} catch (Exception e) {
				Log.e(TAG_KENHNHAC, "err at getResponse-----" + e.getMessage());
				return null;
			}
		} catch (Exception e) {
			Log.e(ERR, "err at account_updateinfo-----" + e.getMessage());
			e.printStackTrace();
		}
		return str;
	}
}
